<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        v-model="searchParams"
        :searchShowList="searchShowList"
        :tableData="tableData"
        :btnList="btnList"
        :tableColumn="tableColumn"
        :total="totalNum"
        requestName="getListHandle"
        @upload="operateHandle($event, 'upload')"
        @rowDown="operateHandle($event, 'rowDown')"
      />
    </div>
    <TrEdit
      v-model="isShowEditPopup"
      title="上传APK "
      :list="popupList"
      :valueObj.sync="formObj"
      :width="480"
      @close="isShowEditPopup = false"
      @confrim="confrimHandle"
    />
  </div>
</template>

<script>
import { getList, upload } from "@/api/system/mobileapk";
import { comExportFileByUrl } from "@/utils/util";
import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrEdit from "@/components/Tr-edit/Tr-edit-popup.vue";
export default {
  name: "mobileapkBox",
  components: {
    TrTable,
    TrEdit,
  },
  data: () => ({
    totalNum: 0,
    tableData: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    searchShowList: [
      {
        label: "附件名称",
        type: "input",
        width: 33.3,
        value: "",
        id: "fileName",
      },
      {
        label: "附件原名",
        type: "input",
        width: 33.3,
        value: "",
        id: "fileOriginName",
      },
      {
        label: "版本号",
        type: "input",
        width: 33.3,
        value: "",
        id: "version",
      },
    ],
    tableColumn: [
      {
        type: "text",
        label: "附件地址",
        value: "link",
      },
      {
        type: "text",
        label: "附件名称",
        value: "name",
      },
      {
        type: "text",
        label: "附件原名",
        value: "originalName",
      },
      {
        type: "text",
        label: "版本号",
        value: "version",
      },
      {
        type: "text",
        label: "更新内容",
        value: "remark",
      },
      {
        type: "option",
        label: "操作",
        showBtnList: [
          // {
          //   label: "删除",
          //   option: "rowDelete",
          // },
          {
            label: "下载",
            option: "rowDown",
          },
        ],
      },
    ],
    btnList: [
      {
        label: "上传APK",
        option: "upload",
        color: "blue",
      },

      // {
      //   label: "删除",
      //   option: "delete",
      //   color: "white",
      //   isRelaTable: true,
      // },
    ],
    isShowEditPopup: false,
    popupList: [
      {
        label: "上传安装包",
        type: "upload",
        width: 100,
        value: [],
        id: "file",
        fileType: ".apk",
        limitNum: 1,
        uploadTip: "",
      },
      {
        label: "版本号",
        type: "input",
        width: 100,
        value: "",
        id: "version",
        inputType: "number",
      },
      {
        label: "更新内容",
        type: "textarea",
        width: 100,
        value: "",
        id: "remark",
      },
    ],
    formObj: {},
    lastVersion: 0,
  }),
  methods: {
    async operateHandle(val, type) {
      console.log(val, type, "typetype");
      switch (type) {
        case "upload":
          this.isShowEditPopup = true;
          break;
        case "rowDown":
          if (val.link == "-") return this.$message.warning("暂无下载地址");
          comExportFileByUrl(val.link, val.name);
          break;
      }
    },
    getListHandle() {
      getList(this.searchParams).then((res) => {
        if (res.data.code != 200) return this.$message.error(res.data.msg);
        let data = res.data.data;
        this.$validateData(data);
        this.totalNum = data.total;
        this.tableData = data.records;
        const last = this.tableData[0];
        if (last.version != "-") {
          this.lastVersion = last.version;
        }
      });
    },
    async confrimHandle() {
      console.log(this.formObj, "this.formObj");
      if (Number(this.lastVersion) >= Number(this.formObj.version))
        return this.$message.warning("版本号必须大于" + this.lastVersion);
      const { file, remark, version } = this.formObj;
      let params = {
        file: file[0],
        remark,
        version,
        bucketName: "mes-frontend",
      };
      let res = await upload(params);
      if (res.data.code != 200) return this.$message.error(res.data.msg);
      this.$message.success(this.$t("msg.success"));
      this.isShowEditPopup = false;
      this.getListHandle();
    },
  },
};
</script>

<style></style>
